<template>
  <div class="writeFileName">
    <el-dialog
      :title="writeFileNameText"
      center
      top="20vh"
      :visible.sync="writeFileNameShow"
      width="30%"
      :append-to-body="true"
      :close-on-click-modal="false"
      :before-close="handleCancel"
    >
      <div>
        <el-form :model="form" label-width="80px" :rules="rules" ref="ruleForm" @keydown.enter.stop.native.prevent>
          <el-form-item label="文件名" prop="value">
            <el-input v-model="form.value" size="mini" clearable placeholder="请输入文件名"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        value: "",
      },
      // todo 不能输入特殊字符
      rules: {
        value: [
          { required: true, message: "请输入文件名", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {
    writeFileNameShow: Boolean,
    writeFileNameText: String
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("handleConfirm", this.form.value);
        } else {
          return false;
        }
      });
    },
    handleConfirm() {
      this.submitForm("ruleForm")
      this.resetForm("ruleForm")
    },
    handleCancel() {
      this.$emit("handleCancel");
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
};
</script>

<style lang="stylus" scoped></style>